Lesson 1 - Reading source code

When you are trying to learn a new langauge, say French or German, one of the first things you will be asked to do is to translate simple sentances into English. Over time the text you can translate will become more complex and will cement your previous learning. The only way to become a good programmer is to translate code and then understand what that code does. One of the first skills you need to develop is to translate source code into English.

When you see source code you need to build a narrative in your head of what the code is trying to say. This is made much easier if the programmer has used good programming practices (see related). Even though code may be translated into English it does not mean that purpose behind the code will be understood. When studying texts (in any language) this is known as comprehension. Thankfully, like maths, soruce code will only even have one meaning. This makes it a bit easier than understanding a classic!


f = 42
c = (5/9) * (f - 32)
print c

Code is read from top to bottom unless a function is encountered (you can think of a function like a page reference!). You must undertand the order that expressions are evaluated in order to correctly translate the code. So the above code would be translated to -

  1. variable f is assigned the value 42
  2. the result of 5/9 is calculated which is then multipylied by f - 32. The result is assigned to c
  3. the final value of c is printed

The expression on line 2 is evaluated in a specific order and this is shown in the translation. So what does this mean? At this stage comprehension is not the goal! You may of realised that this is converting 42 degrees farenheit into celcius (5.6). But do not worry if you did not see this.


x = 1
temp = "*"
while x < = 5
	print temp
	temp = temp + "*"
	x = x + 1

There is a difference between tracing through code and simply translating it. The above code is translated to -

  1. store 1 in x
  2. store "*" in temp
  3. keep looping while x is less than or equal to 5
  4. print temp
  5. add an extra * to temp
  6. add one to x
  7. jump back to line 3

This is written a little less formally than the last example. This is to make it more readable for humans. The whole point of translating is to ensure that the code is undertsandable by humans. If it is not then what is the point! Line 5 adds a little comprehension to the translation. You may not be able to see what the code does at this stage but you may be able to work out what a single line of code will do. This reads better than "variable temp is assigned the value of temp plus *".

Loops will mean a jump. Numbering lines means that it is easy to say "jump to line x". Try and number your translations!!


x = 3
y = 5
if x < y and y > 1:
	print y
else:
	print x

If statements can have both a true and false block. This can be translated as follows -

  1. store 3 in x
  2. store 5 in y
  3. if x is less than y and if y is bigger than 1 then print y
  4. otherwise print x

The translation is fairly straight forward. The tricky part is the conditional expression.